package com.ruoyi.crm.sale.controller;

import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.sale.domain.CrmContract;
import com.ruoyi.crm.sale.service.ICrmContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同管理Controller
 * 
 * @author ruoyi
 * @date 2021-09-18
 */
@RestController
@RequestMapping("/crm/contract")
public class CrmContractController extends BaseController
{
    @Autowired
    private ICrmContractService crmContractService;

    /**
     * 查询合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmContract crmContract)
    {
        startPage();
//        System.out.println("===============================================================");
        List<CrmContract> list = crmContractService.selectCrmContractList(crmContract);
        return getDataTable(list);
    }

    /**
     * 导出合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:export')")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CrmContract crmContract)
    {
        List<CrmContract> list = crmContractService.selectCrmContractList(crmContract);
        ExcelUtil<CrmContract> util = new ExcelUtil<CrmContract>(CrmContract.class);
        return util.exportExcel(list, "合同管理数据");
    }

    /**
     * 获取合同管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmContractService.selectCrmContractById(id));
    }

    /**
     * 获取合同管理-产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:query')")
    @GetMapping(value = "/selectCrmProContractById")
    public AjaxResult selectCrmProContractById(CrmContract crmContract)
    {
        return AjaxResult.success(crmContractService.selectCrmProContractById(crmContract));
    }

    /**
     * 新增合同管理
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmContract crmContract)
    {
        return toAjax(crmContractService.insertCrmContract(crmContract));
    }

    /**
     * 修改合同管理
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmContract crmContract)
    {
        return toAjax(crmContractService.updateCrmContract(crmContract));
    }

    /**
     * 合同提交审核
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/submitCheckStatus")
    public AjaxResult submitCheckStatus(@RequestBody CrmContract crmContract)
    {
        return toAjax(crmContractService.submitCheckStatus(crmContract));
    }

    /**
     * 删除合同管理
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(crmContractService.deleteCrmContractByIds(ids));
    }

    /**
     * 查询已签合同金额
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:list')")
    @Log(title = "查询已签合同金额", businessType = BusinessType.OTHER)
    @PostMapping("/selContractCount")
    public AjaxResult selContractCount(@RequestBody Map<String, Object> params)
    {
        return AjaxResult.success(crmContractService.selContractCount(params));
    }

    /**
     * 查询未审合同数量
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:list')")
    @Log(title = "查询未审合同数量", businessType = BusinessType.OTHER)
    @PostMapping("/selNoCheckConCount")
    public AjaxResult selNoCheckConCount(@RequestBody Map<String, Object> params)
    {
        return AjaxResult.success(crmContractService.selNoCheckConCount(params));
    }

    /**
     * 查询销售额度统计
     */
    @PreAuthorize("@ss.hasPermi('crm:contract:list')")
    @Log(title = "查询销售额度统计", businessType = BusinessType.OTHER)
    @PostMapping("/selSaleQuotaCount")
    public AjaxResult selSaleQuotaCount(@RequestBody Map<String, Object> params)
    {
        return AjaxResult.success(crmContractService.selSaleQuotaCount(params));
    }
}
